//
//  GLMainViewController.m
//  GLSliderView
//
//  Created by 张国梁 on 15/11/28.
//  Copyright © 2015年 CoderGL. All rights reserved.
//

#import "GLMainViewController.h"

#import "GLLeftMenuController.h"
#import "GLRightMenuController.h"

#import "GLNavigationController.h"
#import "GLItem.h"


#define GLAnimateDuritation 0.5

#define GLLeftMenuW 250
#define GLRightMenuW 300


@interface GLMainViewController () <GLLeftMenuControllerDelegate>
/** 导航控制器 */
@property (nonatomic, weak) GLNavigationController *navC;
/** 左边菜单控制器*/
@property (nonatomic, weak) GLLeftMenuController *leftmenuVc;

@property (nonatomic, weak) GLRightMenuController *rightmenuVc;

@end

@implementation GLMainViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBackground];
    
    // 创建左边菜单
    [self setupLeftMenu];
    
    // 创建右边菜单
    [self setupRightMenu];
}

- (void)setupBackground {
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.jpg"]];
    bgView.frame = self.view.bounds;
    [self.view addSubview:bgView];
    
}


- (void)setupTitle:(NSString *)title childVc:(UIViewController *)childVc {
    
    childVc.view.backgroundColor = GLRandomColor;
    
    childVc.title = title;
    childVc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"top_navigation_infoicon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftMenuClick)];
    childVc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"top_navigation_menuicon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(rightMenuClick)];
    
//    创建一个包装导航控制器的子控制器
    GLNavigationController *nav = [[GLNavigationController alloc] initWithRootViewController:childVc];
    
    [self addChildViewController:nav];
    [self.view addSubview:nav.view];
    
    // 记录一下当前的navC
    self.navC = nav;
}

// 设置左边菜单
- (void)setupLeftMenu {
    
    GLLeftMenuController *leftmenuVc = [GLLeftMenuController new];
    leftmenuVc.delegate = self;
    [self addChildViewController:leftmenuVc];
    self.leftmenuVc = leftmenuVc;
    leftmenuVc.view.frame = CGRectMake(-GLLeftMenuW, 0, GLLeftMenuW, SCREEN_HEIGHT);
    [self.view insertSubview:leftmenuVc.view atIndex:1];
}

- (void)leftMenu:(GLLeftMenuController *)leftmenu didClick:(GLItem *)item {
    if (item.destVc == nil) return;
    
    // 先移除旧的控制器 和 View
    GLNavigationController *oldNav = self.navC;
    [oldNav.view removeFromSuperview];
    [oldNav removeFromParentViewController];
    
    // 添加新的控制器 和 View
    [self setupTitle:item.title childVc:item.destVc];

    GLNavigationController *newNav = self.navC;
    newNav.view.transform = oldNav.view.transform;
    
    [UIView animateWithDuration:GLAnimateDuritation animations:^{
        newNav.view.transform = CGAffineTransformIdentity;
        self.leftmenuVc.view.transform = CGAffineTransformIdentity;
    }];
    
}

- (void)leftMenuClick {
    
    [UIView animateWithDuration:GLAnimateDuritation animations:^{
        // 计算缩放比例
        CGFloat scaleNavH = SCREEN_HEIGHT - 2 * NAV_HEIGHT;
        CGFloat scale = scaleNavH / SCREEN_HEIGHT;
        
        // 开始缩放
        self.navC.view.transform = CGAffineTransformMakeScale(scale, scale);
        CGFloat leftMargin = GLLeftMenuW - (SCREEN_WIDTH - SCREEN_WIDTH * scale) / 2;
        // 基于缩放的基础上 进行平移
        self.navC.view.transform = CGAffineTransformTranslate(self.navC.view.transform, leftMargin/scale, 0);
        
        
        self.leftmenuVc.view.transform = CGAffineTransformMakeTranslation(GLLeftMenuW, 0);
        
        
        // 添加一个遮盖
        UIButton *coverBtn = [[UIButton alloc] initWithFrame:self.navC.view.bounds];
        [self.navC.view addSubview:coverBtn];
        [coverBtn addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
    }];
}

- (void)coverClick:(UIButton *)sender {
    
    [UIView animateWithDuration:GLAnimateDuritation animations:^{
        self.navC.view.transform = CGAffineTransformIdentity;
        self.leftmenuVc.view.transform = CGAffineTransformIdentity;
        self.rightmenuVc.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [sender removeFromSuperview];
    }];
    
}


- (void)setupRightMenu {
    GLRightMenuController *rightVc = [GLRightMenuController new];
    [self addChildViewController:rightVc];
    self.rightmenuVc = rightVc;
    rightVc.view.frame = CGRectMake(SCREEN_WIDTH, 0, GLRightMenuW, SCREEN_HEIGHT);
    [self.view insertSubview:rightVc.view atIndex:1];
}


- (void)rightMenuClick {
    [UIView animateWithDuration:GLAnimateDuritation animations:^{
        // 计算缩放比例
        CGFloat scaleNavH = SCREEN_HEIGHT - 2 * NAV_HEIGHT;
        CGFloat scale = scaleNavH / SCREEN_HEIGHT;
        
        // 开始缩放
        self.navC.view.transform = CGAffineTransformMakeScale(scale, scale);
        CGFloat rightMargin = -(GLRightMenuW - (SCREEN_WIDTH - SCREEN_WIDTH * scale) / 2);
        // 基于缩放的基础上 进行平移
        self.navC.view.transform = CGAffineTransformTranslate(self.navC.view.transform, rightMargin/scale, 0);
        
        
        self.rightmenuVc.view.transform = CGAffineTransformMakeTranslation(-GLRightMenuW, 0);
        
        // 添加一个遮盖
        UIButton *coverBtn = [[UIButton alloc] initWithFrame:self.navC.view.bounds];
        [self.navC.view addSubview:coverBtn];
        [coverBtn addTarget:self action:@selector(coverClick:) forControlEvents:UIControlEventTouchUpInside];
    } completion:^(BOOL finished) {
        [self.rightmenuVc didShow];
    }];
}


@end
